package hn.cch.spring.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.util.Locale;

public class MessageSourceDemo {

    private static final Logger logger = LoggerFactory.getLogger(MessageSourceDemo.class);


    public static void main(String[] args) {
        // 国际化
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        // new Supplier<MessageSource>()
        applicationContext.registerBean("messageSource", MessageSource.class, () -> {
            ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
            ms.setDefaultEncoding("utf-8");
            ms.setBasename("messages");
            return ms;
        });
        applicationContext.refresh();
        // 国际化：message-en.properties，message-zh.properties
        String english = applicationContext.getMessage("hi", null, Locale.ENGLISH);
        logger.info("english:{}", english);
        String chinese = applicationContext.getMessage("hi", null, Locale.CHINESE);
        logger.info("chinese:{}", chinese);

    }
}
